package com.usian.admin.service.imlp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdSensitiveMapper;
import com.usian.admin.service.AdSensitiveService;
import com.usian.model.admin.dtos.SensitiveDto;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {


    /**
     * 根据名称分页查询敏感词
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(SensitiveDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult (AppHttpCodeEnum.PARAM_INVALID);
        }

        dto.checkParam ();

        LambdaQueryWrapper<AdSensitive> adSensitiveLambdaQueryWrapper = new LambdaQueryWrapper<> ();

        if (StringUtils.isNotBlank (dto.getName ())) {
            adSensitiveLambdaQueryWrapper.like (AdSensitive::getSensitives, dto.getName ());
        }

        Page<AdSensitive> page = new Page<> (dto.getPage (), dto.getSize ());
        IPage<AdSensitive> sensitiveIPage = this.page (page, adSensitiveLambdaQueryWrapper);
        if (null == sensitiveIPage) {
            return ResponseResult.errorResult (AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        PageResponseResult responseResult = new PageResponseResult (dto.getPage (), dto.getSize (), (int) sensitiveIPage.getTotal ());
        responseResult.setData (sensitiveIPage.getRecords ());
        return responseResult;
    }

    /**
     * 新增
     *
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        if (null == adSensitive) {
            return ResponseResult.errorResult (AppHttpCodeEnum.PARAM_INVALID);
        }

        adSensitive.setCreatedTime (new Date ());

        boolean save = this.save (adSensitive);
        if (!save) {
            return ResponseResult.errorResult (AppHttpCodeEnum.SAVE_FAIL);
        }
        return ResponseResult.okResult (AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改
     *
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        if (null == adSensitive || null == adSensitive.getId ()) {
            return ResponseResult.errorResult (AppHttpCodeEnum.PARAM_INVALID);
        }
        boolean updateById = this.updateById (adSensitive);
        if (!updateById) {
            return ResponseResult.errorResult (AppHttpCodeEnum.UPDATE_FAIL);
        }
        return ResponseResult.okResult (AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除敏感词
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(Integer id) {
        if (null == id){
            return ResponseResult.errorResult (AppHttpCodeEnum.PARAM_INVALID);
        }


        AdSensitive adSensitive = this.getById (id);
        if (null == adSensitive){
            return ResponseResult.errorResult (AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        boolean remove = this.removeById (id);

        if (!remove){
            return ResponseResult.errorResult (AppHttpCodeEnum.DELETE_FAIL);
        }

        return ResponseResult.okResult (AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public List<String> findAllSensitives() {
        return list ().stream ().map (AdSensitive::getSensitives).collect (Collectors.toList ());
    }
}
